package pro.bbs.view.web;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yy.kit.io.YYIOKits;
import com.yy.kit.log.YYLogKits;
import com.yy.pojo.image.TImages;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import pro.bbs.view.service.IImagesRemoteService;
import pro.bbs.view.web.base.BbsCommonController;

import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <p>
 * 图片的上传及显示controller
 * </p>
 *
 * @author 杨阳
 * @since 2017年10月20日
 */
@Api(value = "图片的上传及显示controller")
@Controller
@RequestMapping("/images")
public class BbsImagesController extends BbsCommonController {

    @Autowired
    private IImagesRemoteService iImagesRemoteService;

    /**
     * <p>
     * 从数据库中获取到图片，显示在页面上
     * </p>
     *
     * @param id       图片id
     * @param response {@link HttpServletResponse}
     * @author 杨阳
     * @since 2017年10月20日
     */
    @RequestMapping(value = "/getimg/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "从数据库中获取到图片，显示在页面上", httpMethod = "GET", notes = "从数据库中获取到图片，显示在页面上,以流的方式写出")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "图片ID", required = true, paramType = "path")
    })
    @HystrixCommand(fallbackMethod = "imagesFallbackMethod")
    public void getImage(@PathVariable("id") String id, HttpServletResponse response) {
        YYLogKits.info(BbsImagesController.class, "┌───────────────────────────────────────────────────────────────────────────┐");
        YYLogKits.info(BbsImagesController.class, "├图片id:{} ", id);
        TImages images = iImagesRemoteService.getImage(id);
        response.setContentType("image/jpeg");
        response.setCharacterEncoding("UTF-8");
        OutputStream outputSream = null;
        InputStream in = null;
        try {
            outputSream = response.getOutputStream();
            in = new ByteArrayInputStream(images.getImage());
            int len;
            byte[] buf = new byte[1024];
            while ((len = in.read(buf, 0, 1024)) != -1) {
                outputSream.write(buf, 0, len);
            }
        } catch (IOException e) {
            YYLogKits.printException(this.getClass(), e.getMessage());
        } finally {
            YYIOKits.close(outputSream);
            YYIOKits.close(in);
        }
        YYLogKits.info(BbsImagesController.class, "├图片md5:{} ", images.getMd5());
        YYLogKits.info(BbsImagesController.class, "└───────────────────────────────────────────────────────────────────────────┘");
    }

    /**
     * <p>
     * 调用远程服务，获取图片失败的回调方法
     * </p>
     *
     * @param id 图片id
     * @author 杨阳
     * @since 2017年11月3日
     */
    public void imagesFallbackMethod(String id) {
        YYLogKits.info(BbsImagesController.class, "┌───────────────────────────────────────────────────────────────────────────┐");
        YYLogKits.info(BbsImagesController.class, "├图片id:{} ", id);
        YYLogKits.info(BbsImagesController.class, "├远程图片服务异常，请检查!!!!");
        YYLogKits.info(BbsImagesController.class, "└───────────────────────────────────────────────────────────────────────────┘");
    }

}
